<?php


namespace app\queue\stomp;


use app\queue\BaseConsumer;
use common\model\TaskModel;
use common\service\CountPendingService;

/**
 * 记录未处理数据统计数据信息
 * Class CountPendingConsumer
 */
class CountPendingConsumer extends BaseConsumer
{
    // 要消费的队列名
    public $queue = 'count_pending';

    // 连接名，对应 config/stomp_queue.php 里的连接`
    public $connection = 'default';

    public function doConsume($data)
    {
        $countPendingService = new CountPendingService();
        try {
            switch ($data['type']) {
                case 'assign_task':
                    // 任务待分配状态，清单生成任务数据，提供任务id
                    switch ($data['mode']) {
                        case 'add':
                            // 新增
                            $countPendingService->assignTaskAdd($data['data']['id']);
                            break;
                        case 'add_batch':
                            // 批量新增
                            foreach ($data['data'] as $id) {
                                $countPendingService->assignTaskAdd($id);
                            }
                            break;
                        case 'add_by_filter':
                            // 批量新增通过过滤条件
                            $taskIdData = model(TaskModel::class)->field('id')->where($data['data'])->select();
                            foreach ($taskIdData as $taskIdItem) {
                                $countPendingService->assignTaskAdd($taskIdItem['id']);
                            }
                            break;
                        case 'sub':
                            // 处理完减少
                            $countPendingService->assignTaskSub($data['data']['id']);
                            break;
                    }
                    break;
                case 'review_submit':
                    // 组长审核完毕后待审核状态
                    switch ($data['mode']) {
                        case 'add':
                            // 新增
                            $countPendingService->reviewSubmitAdd($data['data']['id']);
                            break;
                        case 'sub_batch':
                            // 批量减少
                            foreach ($data['data'] as $id) {
                                $countPendingService->reviewSubmitSub($id);
                            }
                            break;
                    }
                    break;
                case 'review_submit_feedback':
                    // 审核不通过待制片处理
                    switch ($data['mode']) {
                        case 'add':
                            // 新增
                            $countPendingService->reviewSubmitFeedbackAdd($data['data']['id']);
                            break;
                        case 'sub':
                            // 处理完减少
                            $countPendingService->reviewSubmitFeedbackSub($data['data']['id']);
                            break;
                    }
                    break;
            }
        } catch (\Throwable $e) {
            trace(format_exception_info($e), 'ERR');
        }
    }
}
